package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.PharmaceuticalCareDetailBo;
import com.hospital.workstation.domain.vo.PharmaceuticalCareDetailVo;
import com.hospital.workstation.domain.PharmaceuticalCareDetail;
import com.hospital.workstation.mapper.PharmaceuticalCareDetailMapper;
import com.hospital.workstation.service.IPharmaceuticalCareDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 药学监护详情Service业务层处理
 *
 * @author whb
 * @date 2023-02-15
 */
@RequiredArgsConstructor
@Service
public class PharmaceuticalCareDetailServiceImpl implements IPharmaceuticalCareDetailService {

    private final PharmaceuticalCareDetailMapper baseMapper;

    /**
     * 查询药学监护详情
     */
    @Override
    public PharmaceuticalCareDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药学监护详情列表
     */
    @Override
    public TableDataInfo<PharmaceuticalCareDetailVo> queryPageList(PharmaceuticalCareDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PharmaceuticalCareDetail> lqw = buildQueryWrapper(bo);
        Page<PharmaceuticalCareDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药学监护详情列表
     */
    @Override
    public List<PharmaceuticalCareDetailVo> queryList(PharmaceuticalCareDetailBo bo) {
        LambdaQueryWrapper<PharmaceuticalCareDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PharmaceuticalCareDetail> buildQueryWrapper(PharmaceuticalCareDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PharmaceuticalCareDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, PharmaceuticalCareDetail::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(bo.getCareDate() != null, PharmaceuticalCareDetail::getCareDate, bo.getCareDate());
        lqw.eq(bo.getInHospitalDays() != null, PharmaceuticalCareDetail::getInHospitalDays, bo.getInHospitalDays());
        lqw.eq(StringUtils.isNotBlank(bo.getVitalSigns()), PharmaceuticalCareDetail::getVitalSigns, bo.getVitalSigns());
        lqw.eq(StringUtils.isNotBlank(bo.getExecutiveCondition()), PharmaceuticalCareDetail::getExecutiveCondition, bo.getExecutiveCondition());
        lqw.eq(StringUtils.isNotBlank(bo.getSchemeAdjustment()), PharmaceuticalCareDetail::getSchemeAdjustment, bo.getSchemeAdjustment());
        lqw.eq(StringUtils.isNotBlank(bo.getCarePlanAdjustment()), PharmaceuticalCareDetail::getCarePlanAdjustment, bo.getCarePlanAdjustment());
        return lqw;
    }

    /**
     * 新增药学监护详情
     */
    @Override
    public Boolean insertByBo(PharmaceuticalCareDetailBo bo) {
        PharmaceuticalCareDetail add = BeanUtil.toBean(bo, PharmaceuticalCareDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改药学监护详情
     */
    @Override
    public Boolean updateByBo(PharmaceuticalCareDetailBo bo) {
        PharmaceuticalCareDetail update = BeanUtil.toBean(bo, PharmaceuticalCareDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PharmaceuticalCareDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药学监护详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
